package com.china08.yxyapi.api.manage.tec.teaching.resource.myresource;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.yxyresource.YxyResourceService;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class ResourceUploadController extends YxyApiBaseController {

    @Autowired
    private YxyResourceService yrs;

    @ApiOperation(value = "上传资源")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/resource/myresource/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        RespModel model = new RespModel();

        List<String> list = reqModel.bookContentList;
        Set<String> set = list.stream().filter(s -> StringUtils.isNotBlank(s)).collect(Collectors.toSet());
        List<String> list2 = new ArrayList<String>(set);
        Assert.isTrue(CollectionUtils.isNotEmpty(list2), "请选择章节");
        String userId = getProfile(request, response).getId();
        String userNick = getProfile(request, response).getUserNick();
        String schoolName = getProfile(request, response).getSchoolName();
        String schoolId = getProfile(request, response).getSchoolId();
        String provinceCode = getProfile(request, response).getSchoolAreaIdProvince();
        String cityCode = getProfile(request, response).getSchoolAreaIdCity();
        String districtCode = getProfile(request, response).getSchoolAreaIdDistrict();
        yrs.addRes(reqModel.viewScope, userId, userNick, schoolId, schoolName, list2, reqModel.intro,
                reqModel.resources, provinceCode, cityCode, districtCode);
        model.setMessage("成功");
        return model;
    }

    public static class ResourceReqModel {
        public String title;
        public String md5; //
        public String location; // 文件全路径
        public String type; // 类型
        public Long size;

        @Override
        public String toString() {
            return "ResourceReqModel [title=" + title + ", md5=" + md5 + ", location=" + location + ", type=" + type
                    + "]";
        }

    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 查看范围 0：私有 1：学校 2： 教育云
        public Integer viewScope = 2;

        // 资源
        public List<ResourceReqModel> resources;

        // 简介
        public String intro = "";

        // 章节目录
        public List<String> bookContentList;

        @Override
        public void autoCheck() {
            Assert.isTrue(viewScope == 0 || viewScope == 1 || viewScope == 2, "非法范围请刷新页面");
            Assert.isTrue(bookContentList != null && bookContentList.size() > 0, "请选择目录");
            Assert.isTrue(resources != null && resources.size() > 0, "请上传资源");
            Assert.isTrue(intro.length() <= 140, "简介字数超限最大140字");
        }

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
